package com.cskaoyan.service.wx.footprint;

import com.cskaoyan.bean.MarketFootprint;
import com.cskaoyan.bean.MarketFootprintExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.wx.footprint.WxFootprintListVo;
import com.cskaoyan.bean.vo.wx.auth.WxUserUtil;
import com.cskaoyan.bean.vo.wx.footprint.FootprintListVo;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/10/10:38
 * @Description:
 */
@Service
public class WxFootprintServiceImpl implements WxFootprintService {
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    /**
    * @Author: 宾文杰 @Description: 获取用户全部足迹信息 @Date: 2022/9/10
    */
    @Override
    public FootprintListVo list(Integer page, Integer limit) {
        //用户主体
        Subject subject = SecurityUtils.getSubject();
        //用户信息
        MarketUser user = null;
        try {
            user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        } catch (Exception e) {
            e.printStackTrace();
        }
        FootprintListVo footprintListVo = new FootprintListVo();
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        PageHelper.startPage(page,limit);
        List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(footprintExample);
        PageInfo info = new PageInfo(marketFootprints);
        ArrayList<WxFootprintListVo> footprintListVos = new ArrayList<>();
        for (MarketFootprint marketFootprint : marketFootprints) {
            WxFootprintListVo wxFootprintListVo = new WxFootprintListVo();
            Integer goodsId = marketFootprint.getGoodsId();

            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
            wxFootprintListVo.setAddTime(marketFootprint.getAddTime());
            wxFootprintListVo.setBrief(marketGoods.getBrief());
            wxFootprintListVo.setGoodsId(goodsId);
            wxFootprintListVo.setId(marketFootprint.getId());
            wxFootprintListVo.setName(marketGoods.getName());
            wxFootprintListVo.setPicUrl(marketGoods.getPicUrl());
            wxFootprintListVo.setRetailPrice(marketGoods.getRetailPrice());
            footprintListVos.add(wxFootprintListVo);
        }
            footprintListVo.setLimit(limit);
            footprintListVo.setList(footprintListVos.toArray());
            footprintListVo.setPage(page);
            footprintListVo.setPages(info.getPages());
            footprintListVo.setTotal(info.getTotal());
        return footprintListVo;
    }

    /**
    * @Author: 宾文杰 @Description: 删除足迹 @Date: 2022/9/11
    */
    @Override
    public void delete(Integer id) {
        MarketUser user = WxUserUtil.userUtil();
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        criteria.andIdEqualTo(id);
        footprintMapper.deleteByExample(footprintExample);
    }
}
